#include "AudioChannelObserver.h"
#include <pthread.h>
#include "Log.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/system_properties.h>


void (*pOnAudioChannelChanged)(int channel );
pthread_t 		m_AudioChannelTid;
pthread_t *		m_AudioChannelThreadPtr;

pthread_t 		m_AudioStopTid;
pthread_t *		m_AudioStopThreadPtr;

int currentChannel = 0; //current audio channel
int lastChannel = 0; //last audio channel
volatile int keepRunning = 1;

volatile int keepObserveAudioStopRunning = 0;

//#define AUDIO_CHANNEL_PROP_KEY "suding.rplay.audio"
#define AUDIO_CHANNEL_PROP_KEY "sys.lynx.rplay.audio"


void *audioChannelThread(void *arg) {
	//第一次延迟4S
	usleep(4000*1000);//4s
	char	channel [ 100 ] = { 0 };
	char	audioBorrow [ 100 ] = { 0 };
	keepRunning = 1;
	while ( keepRunning ){

		memset(channel,0,100);
		__system_property_get(AUDIO_CHANNEL_PROP_KEY, channel);
		if(strlen(channel)==0){
			strncpy(channel,"0",strlen("0"));
		}
		int newChannel = atoi( channel );
		if( newChannel != currentChannel )
		{
			currentChannel = newChannel;
			LOGD("the app set new audio channel = %d \n", currentChannel);
			if(pOnAudioChannelChanged != NULL)
			{
				if( newChannel == 0 && lastChannel != 1)
				{
					//延迟stop
					keepObserveAudioStopRunning = 1;
				}else{
					lastChannel = currentChannel;
					keepObserveAudioStopRunning = 0;
					LOGD("audioChannelThread notify the protocol new audio channel = %d \n", currentChannel);
//					__system_property_get("sys.shark.audio.borrow", audioBorrow);
//					while( strcmp(audioBorrow, "1") == 0 ){
//						__system_property_get("sys.shark.audio.borrow", audioBorrow);
//						usleep(100*1000);
//					}
					pOnAudioChannelChanged(currentChannel);
				}

			}
		}
		usleep(700*1000);
	}

	return (NULL);
}

void *audioStopThread(void *arg) {
	char	channel [ 100 ];
	keepRunning = 1;
	while ( keepRunning ){
//		LOGD("audioStopThread running...\n");
		while ( keepObserveAudioStopRunning ){
			memset(channel,0,100);
			__system_property_get(AUDIO_CHANNEL_PROP_KEY, channel);
			if(strlen(channel)==0){
				strncpy(channel,"0",strlen("0"));
			}
			int newChannel = atoi( channel );
			if( newChannel != lastChannel )
			{
				if(lastChannel == 4 && newChannel == 0)
				{
					usleep(5000*1000); //5S
					if(currentChannel != 0)
					{
						continue;
					}
				}
				lastChannel = newChannel;
				LOGD("audioStopThread the app set new audio channel = %d \n", currentChannel);
				if(pOnAudioChannelChanged != NULL)
				{
					if( newChannel == 0 )
					{
						LOGD("audioStopThread notify the protocol new audio channel = %d \n", currentChannel);
						pOnAudioChannelChanged(currentChannel);
					}

				}
			}
			usleep(20*1000); //20ms
		}
		usleep(3000*1000); //3S
	}

	return (NULL);
}


void start_observe_audio_channel(void (*pFunc)(int channel)){
	pOnAudioChannelChanged = pFunc;
	int ret;
	if ((ret = pthread_create(&m_AudioChannelTid, NULL, audioChannelThread, NULL))
			!= 0) {
		LOGD("observe_audio_channel thread create failed!\n");
	} else {
		m_AudioChannelThreadPtr = &m_AudioChannelTid;
		LOGD("observe_audio_channel thread create successed!\n");
	}

	int ret2;
	if ((ret2 = pthread_create(&m_AudioStopTid, NULL, audioStopThread, NULL))
			!= 0) {
		LOGD("observe_audio_stop thread create failed!\n");
	} else {
		m_AudioStopThreadPtr = &m_AudioStopTid;
		LOGD("observe_audio_stop thread create successed!\n");
	}
}


void cancel_observe_audio_channel( ){
	keepObserveAudioStopRunning = 0;
	keepRunning = 0;
	if( m_AudioChannelThreadPtr !=NULL )
	{
		pthread_join(m_AudioChannelTid , NULL);
		m_AudioChannelThreadPtr = NULL;
	}
	if( m_AudioStopThreadPtr !=NULL )
	{
		pthread_join(m_AudioStopTid , NULL);
		m_AudioStopThreadPtr = NULL;
	}

}


